"""
@-*- coding: utf-8 -*-
@ python: 3.12.3
@ 创建者: JacksonCode
@ 创建时间: 2025/8/22
"""

import requests
import time
import json
from datetime import datetime, timedelta
import subprocess
from functools import partial

subprocess.Popen = partial(subprocess.Popen, encoding="utf-8")

import execjs
import sys

# 设置默认编码为UTF-8
if sys.stdout.encoding != 'utf-8':
    sys.stdout.reconfigure(encoding='utf-8')

# 最简单的版本
now = datetime.now()
# 生成 BeginTime: 今天00:00:00
begin_time = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)
begin_time_str = begin_time.strftime("%Y-%m-%d %H:%M:%S")
# 生成 EndTime: 今天23:59:59
end_time = datetime.now().replace(hour=23, minute=59, second=59, microsecond=0)
end_time_str = end_time.strftime("%Y-%m-%d %H:%M:%S")

headers = {
    "Accept": "application/json, text/plain, */*",
    "Accept-Language": "zh-CN,zh;q=0.9",
    "Cache-Control": "no-cache",
    "Connection": "keep-alive",
    "Content-Type": "application/json;charset=UTF-8",
    "Origin": "https://ggzyfw.fujian.gov.cn",
    "Pragma": "no-cache",
    "Referer": "https://ggzyfw.fujian.gov.cn/business/list/",
    "Sec-Fetch-Dest": "empty",
    "Sec-Fetch-Mode": "cors",
    "Sec-Fetch-Site": "same-origin",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/139.0.0.0 Safari/537.36",
    "portal-sign": "161b2522b065fb58f10d2ae44185aba5",
    "sec-ch-ua": "\"Not;A=Brand\";v=\"99\", \"Google Chrome\";v=\"139\", \"Chromium\";v=\"139\"",
    "sec-ch-ua-mobile": "?0",
    "sec-ch-ua-platform": "\"Windows\""
}


def get_sign(data):
    with open("test.js", 'r', encoding="utf-8") as f:
        js_code = f.read()
    ctx = execjs.compile(js_code)
    sign = ctx.call("d", data)
    return sign

# 第二步 解密 decrypt_data
def encrypt(decrypt_data):
    with open("test.js", 'r', encoding="utf-8") as f:
        js_code = f.read()
    ctx = execjs.compile(js_code)
    result = ctx.call("b", decrypt_data)
    return result

def loop_print(table):
    for data in table:
        print(f"名字是:{data["NAME"]}")

def main():
    url = "https://ggzyfw.fujian.gov.cn/FwPortalApi/Trade/TradeInfo"

    for i in range(1, 6):
        data = {
            "pageNo": i,
            "pageSize": 20,
            "total": 2953,
            "AREACODE": "",
            "M_PROJECT_TYPE": "",
            "KIND": "GCJS",
            "GGTYPE": "1",
            "PROTYPE": "",
            "timeType": "6",
            "BeginTime": "2025-02-22 00:00:00",
            "EndTime": "2025-08-22 23:59:59",
            "createTime": "",
            "ts": int(time.time() * 1000)
        }

        # 第一步 抓到他的  加密 data
        json_data = json.dumps(data, separators=(',', ':'))
        headers['portal-sign'] = get_sign(data)
        res = requests.post(url=url, headers=headers, data=json_data)
        time.sleep(1.5)
        decrypt_data = res.json()["Data"]
        encrypt_data = encrypt(decrypt_data)
        json_item = json.loads(encrypt_data)
        Table = json_item["Table"]
        loop_print(Table)


if __name__ == '__main__':
    main()
